home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / REVERSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  885 b   |  35 lines

  1. /* reverse.c - checks a phrase to see if it is a palindrome */
  2. #include "stdio.h"
  3.  
  4. main()
  5.  {
  6.     char phrase[81] ;
  7.     char rev_phrase[81] ;
  8.  
  9.     printf("Type a phrase : \n");/* prompt for phrase from keyboard */
  10.     getstr(phrase,80) ;         /* and get it */
  11.  
  12.     do_reverse(phrase,rev_phrase) ; /* construct reversed phrase */
  13.     if( strcmp(phrase,rev_phrase) == 0 ) /* compare original */
  14.         printf(" ** PALINDROME ** ");
  15.     else printf(" REVERSE = %s",rev_phrase) ;
  16.  }
  17.  
  18. int do_reverse(s1,s2)        /* reverse a string */
  19.   char s1[] ;            /* the string to be reversed */
  20.   char s2[] ;            /* place its reverse here */
  21.   {
  22.     int i , j ;
  23.                 /*copy characters starting at end of s1 */
  24.     i = 0 ;
  25.     j = strlen(s1) - 1 ;    /* find index of last character in s1 */
  26.     while( j >= 0 )
  27.       { s2[i] = s1[j] ;
  28.         i = i + 1 ;
  29.         j = j - 1 ;
  30.       }
  31.     s2[i] = 0 ;        /* mark end of string */
  32.   }
  33.  
  34.  
  35.